package re.kitri.common.game;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import re.kitri.common.Paging.PagingUtil;

/**
 * Servlet implementation class gameServlet
 */
@WebServlet("/gameServlet")
public class gameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public gameServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		receivedProcessing(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		receivedProcessing(request,response);
	}

	private void receivedProcessing(HttpServletRequest request,HttpServletResponse response) {
		String divide = request.getParameter("divide");
		GameDTO gameDTO = new GameDTO();
		GameImple gameImple = new GameImple();

		HttpSession session = request.getSession();
		RequestDispatcher requestDispatcher = null;

		if(divide.equals("game_schedule")){

			gameDTO.setSelectpage(divide);

			int currentPage = 0;
			String url="";
			url = "/gameServlet?divide=game_schedule";
			String currentPageString = request.getParameter("currentPage");

			int totRecord = gameImple.totRecord(gameDTO);

			if(currentPageString == null) currentPage = 1;
			else currentPage = Integer.parseInt(currentPageString);

			PagingUtil pagingUtil = new PagingUtil(url , currentPage, totRecord, 4, 5);

			gameDTO.setStartSeq(pagingUtil.getStartSeq());
			gameDTO.setEndSeq(pagingUtil.getEndSeq());

			String getPagingHtml = pagingUtil.getPagingHtml();

			String userId = session.getAttribute("SESS_USERID").toString();
			if(userId.equals("admin")){
				try {
					requestDispatcher = request
							.getRequestDispatcher("/game/game_schedule_admin.tiles");

					request.setAttribute("gameList", gameImple.gameList(gameDTO));
					request.setAttribute("PagingHtml", getPagingHtml);

					requestDispatcher.forward(request, response);
				} catch (ServletException | IOException e) {
					e.printStackTrace();
				}
			}
			else{
				try {
					requestDispatcher = request
							.getRequestDispatcher("/game/game_schedule.tiles");

					request.setAttribute("gameList", gameImple.gameList(gameDTO));
					request.setAttribute("PagingHtml", getPagingHtml);

					requestDispatcher.forward(request, response);
				} catch (ServletException | IOException e) {
					e.printStackTrace();
				}
			}
		}

		else if(divide.equals("bet_select")){
			//			if(session.getAttribute("SESS_UESRSEQ") != null){

			gameDTO.setSelectpage(divide);
			int currentPage = 0;
			String url="";
			url = "/gameServlet?divide=bet_select";
			String currentPageString = request.getParameter("currentPage");

			int totRecord = gameImple.totRecord(gameDTO);

			if(currentPageString == null) currentPage = 1;
			else currentPage = Integer.parseInt(currentPageString);

			PagingUtil pagingUtil = new PagingUtil(url , currentPage, totRecord, 4, 5);

			gameDTO.setStartSeq(pagingUtil.getStartSeq());
			gameDTO.setEndSeq(pagingUtil.getEndSeq());

			String getPagingHtml = pagingUtil.getPagingHtml();


			try {
				requestDispatcher = request
						.getRequestDispatcher("/game/game_bet_select.tiles");

				request.setAttribute("gameList", gameImple.gameList(gameDTO));
				request.setAttribute("PagingHtml", getPagingHtml);

				requestDispatcher.forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
			//}
			//			else {
			//				requestDispatcher = request.getRequestDispatcher("/game/not_master.tiles");
			//				try {
			//					requestDispatcher.forward(request, response);
			//				} catch (ServletException | IOException e) {
			//					e.printStackTrace();
			//				}
			//			}
		}
		else if(divide.equals("bet_select_info")){
			//	if(session.getAttribute("SESS_UESRSEQ") != null){

			ArrayList gameResultList = new ArrayList();
			gameResultList = (ArrayList)session.getAttribute("gameResultList");
			ArrayList gameSeqList = new ArrayList();
			gameSeqList = (ArrayList)session.getAttribute("gameSeqList");
			gameDTO.setMemberSeq(Integer.parseInt(session.getAttribute("SESS_USERSEQ").toString()));

			session.removeAttribute("gameResultList");
			session.removeAttribute("gameSeqList");

			//			String[] gameSeq = request.getParameterValues("objChkBox_gameSeq");

			gameDTO.setPaymentSeq(Integer.parseInt(request.getParameter("payment_seq")));

			//				gameDTO.setPaymentSeq(gameImple.paymentSelect().getPaymentSeq());
			gameDTO.setAllocation(Integer.parseInt(request.getParameter("money_input")));

			//			for(int i = 0; i < gameSeq.length; i++){
			//				gameSeqList.add(gameSeq[i]);
			//				String gameResult = request.getParameter(gameSeq[i]);
			//				gameResultList.add(gameResult);
			//
			//			}

			int groupSeq = gameImple.selectInsert(gameResultList,gameSeqList,gameDTO);
			gameDTO.setBetGroupSeq(groupSeq);

			gameResultList = gameImple.selectView(gameDTO);

			try {
				requestDispatcher = request
						.getRequestDispatcher("/game/bet_select_info.tiles");
				request.setAttribute("gameSelectList", gameResultList);
				requestDispatcher.forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
			//}
			//			else {
			//				requestDispatcher = request.getRequestDispatcher("/game/not_master.tiles");
			//				try {
			//					requestDispatcher.forward(request, response);
			//				} catch (ServletException | IOException e) {
			//					e.printStackTrace();
			//				}
			//			}
		}

		else if(divide.equals("bet_info")){
			ArrayList gameResultList = new ArrayList();
			gameDTO.setMemberSeq(Integer.parseInt(session.getAttribute("SESS_USERSEQ").toString()));
			gameResultList = gameImple.memberSelectView(gameDTO);
			try {
				requestDispatcher = request
						.getRequestDispatcher("/game/bet_info.tiles");
				request.setAttribute("gameResultList", gameResultList);
				requestDispatcher.forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		}
		else if(divide.equals("bet_info_click")){
			ArrayList gameSelectList = new ArrayList();
			ArrayList gameResultList = new ArrayList();
			gameDTO.setBetGroupSeq(Integer.parseInt(request.getParameter("groupseq")));
			gameSelectList = gameImple.selectView(gameDTO);

			gameResultList = gameImple.selectCompare(gameSelectList);
			try {
				requestDispatcher = request
						.getRequestDispatcher("/game/game_bet_result.tiles");
				request.setAttribute("gameSelectList", gameResultList);
				requestDispatcher.forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		}
		else if(divide.equals("shedule_input")){
			gameDTO.setHomeTeam(request.getParameter("hometeam_select"));
			gameDTO.setAwayTeam(request.getParameter("awayteam_select"));
			gameDTO.setGameCheck(request.getParameter("check_select"));
			gameDTO.setGameResult(request.getParameter("check_result"));
			gameDTO.setGameRegdate(request.getParameter("game_date"));
			gameImple.insertShedule(gameDTO);

			try {
				response.sendRedirect("/gameServlet?divide=game_schedule");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else if(divide.equals("game_Info_delete")){
			System.out.println(request.getParameter("deleteSeq"));
			gameDTO.setGameSeq(Integer.parseInt(request.getParameter("deleteSeq")));
			gameImple.sheduleDelete(gameDTO);

			try {
				response.sendRedirect("/gameServlet?divide=game_schedule");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(divide.equals("update_game_page")){
			int gameSeq = (Integer.parseInt(request.getParameter("game_seq")));
			gameDTO = gameImple.clickSchedule(gameSeq);
			try {
				requestDispatcher = request.getRequestDispatcher("/game/game_schedule_update.tiles");
				request.setAttribute("gameDTO", gameDTO);
				requestDispatcher.forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}

		}else if(divide.equals("update_game")){
			gameDTO.setHomeTeam(request.getParameter("hometeam_select"));
			gameDTO.setAwayTeam(request.getParameter("awayteam_select"));
			gameDTO.setGameCheck(request.getParameter("check_select"));
			gameDTO.setGameResult(request.getParameter("check_result"));
			gameDTO.setGameRegdate(request.getParameter("game_date"));
			gameDTO.setGameSeq(Integer.parseInt(request.getParameter("gameseq")));
			gameImple.sheduleUpdate(gameDTO);

			try {
				response.sendRedirect("/gameServlet?divide=game_schedule");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(divide.equals("input_game")){
			try {
				requestDispatcher = request
						.getRequestDispatcher("/game/game_chedule_input.tiles");
				requestDispatcher.forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		}

		else{
		}
	}

}
